// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import GameData from "./GameData";
import GameUI from "./GameUI";
import { PoolManager } from "./PoolManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Coin extends cc.Component {

    @property
    price = 0;

    isCollected = false;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    protected onEnable(): void {
        this.isCollected = false;
        this.node.children[0].opacity = 0;
        cc.tween(this.node.children[0])
            .delay(GameData.randomRange(0, 3))
            .call(() => {
                this.node.children[0].scale = 0;
                this.node.children[0].opacity = 255;
                cc.tween(this.node.children[0])
                    .to(0.5, {
                        scale: 3,
                        opacity: 0
                    })
                    .delay(5)
                    .call(() => {
                        this.node.children[0].scale = 1;
                        this.node.children[0].opacity = 255;
                    })
                    .union()
                    .repeatForever()
                    .start();
            })
            .start();
    }

    start() {

    }

    // update (dt) {}

    Collect() {
        cc.Tween.stopAllByTarget(this.node.children[0]);
        this.isCollected = true;
        var x = this;
        cc.tween(this.node)
            .to(0.5, { position: GameUI.Instance.coinLabel.node.parent.position })
            .call(() => {
                var num = x.price
                GameUI.Instance.Coins += num;
                PoolManager.putNode(x.node);
            })
            .start();
    }
}
